#include <stdio.h>
#include <stdlib.h>

typedef struct DNode {
	int freq;
	char data;
	struct DNode* prev;
	struct DNode* next;
} DNode, *PDNode;

PDNode head;

void swap(PDNode p) {
	char temp_data;
	int temp_freq;

	PDNode pre = p->prev;

	while (pre != NULL && pre->freq < p->freq) {
		temp_data = p->data;
		temp_freq = p->freq;

		p->data = pre->data;
		p->freq = pre->freq;

		pre->data = temp_data;
		pre->freq = temp_freq;

		p = pre;
		pre = pre->prev;
	}
}

void Search(char tem) {
	PDNode ahead = head;

	while (ahead != NULL) {
		if (tem == ahead->data) {
			ahead->freq++;
			swap(ahead);
		}
		ahead = ahead->next;
	}
}

void Insert_tail(char val) {
	PDNode ahead = head;
	PDNode temp;
	temp = (PDNode)malloc(sizeof(DNode));
	temp->data = val;
	temp->next = NULL;
	temp->prev = NULL;
	temp->freq = 0;
	if (ahead == NULL) {
		head = temp;
		return;
	}
	while (ahead->next != head) {
		ahead = ahead->next;
	}
	ahead->next = temp;
	temp->prev = ahead;
	temp->next = head;
	head->prev = temp;
}

void Print() {
	PDNode temp = head;

	printf("\n");
	do {
		printf("%c ", temp->data);
		temp = temp->next;
	} while (temp != head);
}

int main() {
	head = NULL;
	int m, n;
	scanf("%d %d", &m, &n);

	char val;
	char word;
	for (int i = 0; i < m; i++) {
		scanf(" %c", &val);
		Insert_tail(val);
	}

	for (int j = 0; j < n; j++) {
		scanf(" %c", &word);
		Search(word);
	}

	Print();
	return 0;
}
